package com.bootdo.sell.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.sell.domain.OrderDO;
import com.bootdo.sell.service.OrderService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

/**
 * 订单表
 * 
 * @author mp
 * @email 1992lcg@163.com
 * @date 2020-03-02 09:22:07
 */
 
@Controller
@RequestMapping("/sell/order")
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private FileService sysFileService;
	@Autowired
	private BootdoConfig bootdoConfig;


	@GetMapping()
	@RequiresPermissions("sell:order:order")
	String Order(){
	    return "sell/order/order";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sell:order:order")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OrderDO> orderList = orderService.list(query);
		int total = orderService.count(query);
		PageUtils pageUtils = new PageUtils(orderList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sell:order:add")
	String add(){
	    return "sell/order/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sell:order:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		OrderDO order = orderService.get(id);
		model.addAttribute("order", order);
	    return "sell/order/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sell:order:add")
	public R save( OrderDO order){
		if(orderService.save(order)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sell:order:edit")
	public R update( OrderDO order){
		orderService.update(order);
		return R.ok(order.getId()+"");
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sell:order:remove")
	public R remove( Integer id){
		if(orderService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sell:order:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		orderService.batchRemove(ids);
		return R.ok();
	}


	@RequestMapping("/upfile")
	public String upfile(){
		return "sell/order/upfile";
	}


	/**
	 * 图片修改
	 */
	@ResponseBody
	@RequestMapping("/updatePicture")
	public R updatePicture(@RequestParam(value = "file",required = false) MultipartFile[] files){

		FileDO fileDO = new FileDO();

		String filePath = bootdoConfig.getUploadPath();
		System.out.println("路径:"+filePath);
		for (MultipartFile file:files){
			String oldName = file.getOriginalFilename();
			System.out.println("旧名字是:"+oldName);
			String conversion = oldName.split("\\.")[oldName.split("\\.").length-1];
			String newName = new Date().getTime()+""+(int)(Math.random()*100000)+"."+conversion;
			System.out.println("新名字:"+newName);
			try {
				File file1 = new File(filePath);
				if (!file1.exists()){
					file1.mkdirs();
				}

				String PathName = "/files/"+newName;
				String newFilePath = filePath+newName;
				System.out.println("最后的路径:"+newFilePath);

				fileDO.setUrl(PathName);
				fileDO.setType(1);
				fileDO.setCreateDate(new Date());
				sysFileService.save(fileDO);
				// picture.setPath(PathName);

				file.transferTo(new File(newFilePath));

         /*if (){
            R.ok();
         }*/
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok();
	}
	
}
